<%--
  Created by IntelliJ IDEA.
  User: 施明
  Date: 2019/5/21
  Time: 20:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<c:set var="path" value="${pageContext.request.contextPath}"></c:set>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${path}/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${path}/layuiadmin/style/admin.css" media="all">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>

<body>
<!-- 隐藏表单 修改库存-->
<div id="addStockForm" style="display: none">
    <ins class="adsbygoogle"
         style="display: inline-block; width: 300px; height: 15px"
         data-ad-client="ca-pub-6111334333458862" data-ad-slot="3820120620"></ins>
    <form class="layui-form" action="" lay-filter="example">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">商品ID</label>
                <div class="layui-input-inline">
                    <input type="text" name="goodsid" id="goodsid1" readonly="readonly" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">

                <label class="layui-form-label" >商品库存</label>
                <div class="layui-input-inline">
                    <input type="text" name="goodsnum" id="goodsnum1"  autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品销量</label>
                <div class="layui-input-inline">
                    <input type="text" name="goodssalenum" id="goodssalenum1" readonly="readonly"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="addstockBtn">增加库存</button>
            </div>
        </div>
    </form>
</div>
<!-- 隐藏表单 修改库存-->
<div id="editStockForm" style="display: none">
    <ins class="adsbygoogle"
         style="display: inline-block; width: 300px; height: 15px"
         data-ad-client="ca-pub-6111334333458862" data-ad-slot="3820120620"></ins>
    <form class="layui-form" action="" lay-filter="example">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">商品ID</label>
                <div class="layui-input-inline">
                    <input type="text" name="goodsid" id="goodsid" readonly="readonly" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品库存</label>
                <div class="layui-input-inline">
                    <input type="text" name="goodsnum" id="goodsnum" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品销量</label>
                <div class="layui-input-inline">
                    <input type="text" name="goodssalenum" id="goodssalenum" readonly="readonly"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="stockBtn">提交修改</button>
            </div>
        </div>
    </form>
</div>
<div class="demoTable">
    商品名：
    <div class="layui-inline">
        <input class="layui-input" name="goodsname" id="demoReload"
               autocomplete="off">
    </div>
    <button class="layui-btn" data-type="reload">搜索</button>
</div>
</div>


<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="addGoods">添加商品</button>
    </div>
</script>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改商品信息</a>
    <a class="layui-btn layui-btn-xs" lay-event="editStock">修改库存</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">修改状态</a>
</script>

<script src="${path}/layuiadmin/layui/layui.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use('layedit', function () {
        var layedit = layui.layedit;
        layedit.build('goodsinformation'); //建立编辑器
    });
</script>
<script>


    layui.use(['table', 'form'], function () {

        var table = layui.table;
        var form = layui.form;

        table.render({
            elem: '#test'
            ,
            url: 'toGoodsList'
            ,
            toolbar: '#toolbarDemo',
            title: '用户数据表',
            cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'goodsid', width: 100, title: '商品ID', sort: true},
                    {field: 'twolevelid', width: 100, title: '二级分ID', sort: true, hide: true},
                    {field: 'twolevelname', width: 100, title: '二级分类明', sort: true},
                    {field: 'goodsname', width: 100, title: '商品名称', sort: true},
                    {field: 'goodsprice', width: 100, title: '商品单价', sort: true},
                    {field: 'goodsnum', width: 100, title: '商品库存', sort: true},
                    {field: 'goodssalenum', width: 100, title: '商品销量', sort: true},
                    {field: 'goodstitle', title: '商品标题', width: 150},
                    {field: 'goodsinformation', title: '商品详情', width: 150},
                    {field: 'goodsstatus', title: '状态', width: 150,templet : function (res) {
                        return res.goodsstatus == 0 ? "上架" : "下架";
                  }
                     },
        {
            fixed: 'right', title
        :
            '操作', toolbar
        :
            '#barDemo', width
        :
            250
        }
    ]
    ],
        page: true
            , id
    :
        'testReload'
    })
        ;

//头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;
                case 'addGoods':

                    window.location.href="addGoods";

                    break;

            }
            ;
        });

        //监听行工具事件

        table.on('tool(test)', function (obj) {
            // console.log(obj)
            var data = obj.data;
            if (obj.event === 'del') {
                $.ajax({
                    type: "get",
                    url: "editGoodsSatus",
                    data: {
                        "goodsid": data.goodsid,
                        "goodsstatus": data.goodsstatus,
                    },
                    success: function (data) {
                        if (data.msg == "修改成功") {
                            layer.msg(data.msg, {
                                time: 1000
                            }, function () {
                                location.reload();
                            });
                        } else {
                            layer.msg(data.msg)
                        }
                    }
                });
            } else if (obj.event === 'edit') {
                window.location.href = "editGoods?goodsid=" + data.goodsid;
            } else if (obj.event === 'editStock') {

                $("#goodsid").attr("value", data.goodsid);
                $("#goodsnum").attr("value", data.goodsnum);
                $("#goodssalenum").attr("value", data.goodssalenum);
                var index1 = layer.open({
                    title: "修改库存",
                    skin: 'layui-layer-rim',
                    type: 1,
                    content: $("#editStockForm"),
                });
                //只有加了这一句，表单的复选框，单选框才可以编辑
                layui.form.render();
                //监听提交
                layui.form.on('submit(stockBtn)', function (data2) {
                    var d = data2.field;
                    if (d.goodsnum == "") {
                        layer.alert("商品数量不能为空");
                    } else {
                        $.ajax({
                            type: "get",
                            url: "editGoodsStock",

                            data:{
                                "goodsid":d.goodsid,
                                "goodsnum":d.goodsnum,
                                "goodssalenum":d.goodssalenum
                            },
                            success: function (data) {
                                if (data.msg == "修改成功") {
                                    layer.msg(data.msg, {
                                        time: 1000
                                    }, function () {
                                        layer.close(index1);
                                        location.reload();
                                    });
                                } else {
                                    layer.msg(data.msg)
                                }
                            }
                        });
                    }
                    return false;
                });
            }
        });
        //模糊查询
        var $ = layui.$, active = {
            reload: function () {
                var demoReload = $('#demoReload');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1
                        //重新从第 1 页开始
                    },
                    where: {
                        content: demoReload.val(),
                    }
                });
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

</body>

</html>